package com.lg.concurrent.volatile_detail;

import lombok.SneakyThrows;

/**
 * 注意 while 循环中一定要空
 * 负责写回主存
 */
public class EndLessLoop {
    static class TestC extends  Thread{
        private Integer num;
        private boolean flag = true;
        TestC( int n){
            this.num = n;
        }
        @SneakyThrows
        @Override
        public void run() {
            super.run();
            while (flag) {
//                if (!flag)
//                    break;
//                System.out.println("run while");
//                Thread.sleep(30);
            }
        }
        private  int getNum(){
            return this.num;
        }

        private  void  setFlag (boolean flag){
            this.flag = flag;
        }
    }
    volatile public static  Integer  num =-1;
    @SneakyThrows
    public static void main(String[] args) {

        TestC testC = new TestC(num);
        testC.start();
        Thread.sleep(60);
        testC.setFlag(false);


    }
}
